/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.google
{
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.overlays.PolygonOptions;
	import com.google.maps.overlays.PolylineOptions;
	import com.google.maps.styles.StrokeStyle;

	public class wmgRenderer
	{
		public var innerRenderer:wmgRenderer;
		public var defaultOptions:Object;
		public var selectedOptions:Object;
		public var highlightedOptions:Object;
		public var bothOptions:Object;
		
		public function wmgRenderer()
		{
		}
		
		public function getOptions( overlay:wmgBaseOverlay ): Object {
			var options:Object = this.defaultOptions;
			
			if( this.innerRenderer ){
				options = this.innerRenderer.getOptions( overlay );
			}
			
			if( options == null ){
				if( overlay is wmgMarker){
					options = new MarkerOptions();
				}
				else if( overlay is wmgPolyline ){
					options = new PolylineOptions();
				}
				else if( overlay is wmgPolygon ){
					options = new PolygonOptions();
				}
			}
			
			if( overlay.selected ){
				if( overlay.highlighted && this.bothOptions ){
					options = this.getNewOptions( overlay , options , this.bothOptions );
				}
				else if( this.selectedOptions ){
					options = this.getNewOptions( overlay , options , this.selectedOptions );
				}
			}
			else if( overlay.highlighted && this.highlightedOptions ){
				options = this.getNewOptions( overlay , options , this.highlightedOptions );
			}
			
			return options;
		}
		
		
		protected function calculateColorWithAlpha( top:uint , bottom:uint , alpha:Number ) : uint {
			if( alpha == 1 ){
				return top;
			}
			else if( alpha == 0 ){
				return bottom;
			}
			
			var red:uint = ((((top >> 16) & 0xff) - ((bottom >> 16) & 0xff)) * alpha) + ((bottom >> 16) & 0xff);
			var green:uint = ((((top >> 8) & 0xff) - ((bottom >> 8) & 0xff)) * alpha) + ((bottom >> 8) & 0xff);
			var blue:uint = (((top & 0xff) - (bottom & 0xff)) * alpha) + (bottom & 0xff);
			
			return (((red & 0xff) << 16) | ((green & 0xff) << 8) | (blue & 0xff));
		}
		
		protected function getNewOptions( overlay:wmgBaseOverlay , options:Object , stateOptions:Object ) : Object {
			var newOptions:Object = options;
			

			//var color:uint = this.calculateColorWithAlpha( (stateOptions as Object).color,  (symbol as Object).color , (stateSymbol as Object).alpha );
			
			if( options is MarkerOptions && stateOptions is MarkerOptions ){
				newOptions = MarkerOptions.merge( [options,stateOptions] );
			}
			else if( options is PolylineOptions && stateOptions is PolylineOptions ){
				newOptions = PolylineOptions.merge( [options,stateOptions] );
			}
			else if( options is PolygonOptions && stateOptions is PolygonOptions ){
				newOptions = PolygonOptions.merge( [options,stateOptions] );
			}
			
			return newOptions;
		}
		
	}
}